package com.sudolink.isp.ui.eula;


import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;


/**
 *
 * @author matsu
 */
public class Logger {

    public enum Level {
        DEBUG, INFO, WARNING, SEVERE
    }

    public enum Mode {
        FILE, CONSOLE, OFF
    }

    private static Logger out = null;
    private PrintWriter writer = null;
    private boolean append = true;
    private boolean console = false;
    private Mode mode = Mode.FILE;

    private Logger() {
        setMode(Mode.CONSOLE);
    } //defeat instantiation

    private Logger(PrintWriter writer) {
        this.writer = writer;
    }

    public static void initLogger(String logname, String logpath, boolean append)
            throws IOException {
        //if (out == null) {
        File file = new File(logpath, logname);
        PrintWriter wr = new PrintWriter(
                    new BufferedWriter(
                        new FileWriter(file, append)),true );
        out = new Logger(wr);
        out.info("Initializing Logger.");

        //}
        out.setMode(Mode.FILE);
    }



    public void close() {
        if ( writer != null ) {
            log(Level.DEBUG, "Closing log writer object.");
            writer.close();
        }
    }



    

    public static Logger getLogger() {
        if (out == null) {
            out = new Logger();
            return out;
        } else {
            return out;
        }

    }

    public void write(Level level, String msg) {
        Date now = new Date();
        if ( getMode() == Mode.CONSOLE ) {
            System.out.println( level.toString() + ": " +
                                now.toString() + "-->" + msg);
        } else if ( getMode() == Mode.FILE ) {
            if ( writer != null ) {
                writer.println( level.toString() + ": " +
                                now.toString() + "-->"+ msg);
            }
        }


    }

    public static void log(String msg) {
        Logger logger = getLogger();
        logger.write(Level.INFO, msg);
    }



    public static void log(Level level, String msg) {
        Logger logger = getLogger();
        logger.write(level, msg);
    }

    public static void log(Level level, Exception ex) {
        String newline = System.getProperty("line.separator");
        String stacktrace = "Exception found: " + newline +
                            ex.getMessage() + newline;
        for ( StackTraceElement s : ex.getStackTrace() ) {
            stacktrace += s + newline;
        }
        log(level, stacktrace);
    }

    public static void debug( String msg ) {
        log( Level.DEBUG, msg );
    }

    public static void info( String msg ) {
        log( Level.INFO, msg );
    }

    public static void warning( String msg ) {
        log( Level.WARNING, msg );
    }

    public static void severe( String msg ) {
        log( Level.SEVERE, msg );
    }

    public static void entering( Class clazz, String method ) {
        log( Level.INFO, clazz.getName() + method);
    }

    /**
     * @return the append
     */
    public boolean isAppend() {
        return append;
    }

    /**
     * @param append the append to set
     */
    public void setAppend(boolean append) {
        this.append = append;
    }

//    /**
//     * @return the console
//     */
//    public boolean isConsole() {
//        return console;
//    }
//
//    /**
//     * @param console the console to set
//     */
//    public final void setConsole(boolean console) {
//        this.console = console;
//    }

    public final void setMode( Mode mode ) {
        this.mode = mode;
    }

    public final Mode getMode( ) {
        return mode;
    }
}
